package gindemo

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/opentracing/opentracing-go"
	"github.com/spf13/viper"
	"github.com/uber/jaeger-client-go"
	"github.com/uber/jaeger-client-go/config"
	"io"
)

type ConfigMger struct {
	Mysql MySql  `yaml:"mysql"`
	Redis Redis  `yaml:"redis"`
	Name string  `yaml:"name"`

}
type MySql struct {
	Host string   `yaml:"host"`
	Port int       `yaml:"port"`
}
type Redis struct {
	Host string   `yaml:"host"`
	Port int      `yaml:"port"`
}
var (
	v *viper.Viper
	configobj ConfigMger
)
func initViper()  {
	v = viper.New()
	v.SetConfigFile("./config.yaml")

	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}
	v.WatchConfig()

	v.OnConfigChange(func(e fsnotify.Event) {
		fmt.Println("config file changed:", e.Name)
		if err := v.Unmarshal(&configobj); err != nil {
			fmt.Println(err)
		}
	})
	if err := v.Unmarshal(&configobj); err != nil {
		fmt.Println(err)
	}
	fmt.Println("========>Cofig",configobj)
}

func initJaeger(service string) (opentracing.Tracer, io.Closer) {
	cfg := &config.Configuration{
		Sampler: &config.SamplerConfig{
			Type:  "const",
			Param: 1,
		},
		Reporter: &config.ReporterConfig{
			LogSpans: true,
			LocalAgentHostPort:"192.168.88.104:6831",
		},
	}
	tracer, closer, err := cfg.New(service, config.Logger(jaeger.StdLogger))
	if err != nil {
		panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
	}
	return tracer, closer
}

var (
	tracer,_ = initJaeger("gin的服务")
)


